package com.lyeducation.security.config;

import com.lyeducation.controllers.dto.system.userDetails.UserDetailsImpl;
import com.lyeducation.core.base.SysConstants;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

public class SecurityPrincipalContext {
  public static Authentication getAuthentication() {
    return SecurityContextHolder.getContext().getAuthentication();
  }
  public static UserDetails getUserDetails() {
    Authentication authentication = getAuthentication();
    if (authentication != null) {
      return (UserDetails) authentication.getPrincipal();
    }
    return null;
  }

  public static UserDetailsImpl getUserDetailsImpl() {
    Authentication authentication = getAuthentication();
    if (authentication != null) {
      return (UserDetailsImpl) authentication.getPrincipal();
    }
    return null;
  }

  public static boolean checkHasAdminOrGeneralAdminRole() {
    for (GrantedAuthority grantedAuthority : SecurityPrincipalContext.getUserDetails().getAuthorities()) {
      if (grantedAuthority.getAuthority().equals(SysConstants.PROJECT_ROLE_ADMIN.getValue()) || grantedAuthority.getAuthority().equals(SysConstants.PROJECT_ROLE_GENERALADMIN)) {
        return true;
      }
    }
    return false;
  }

  public static boolean checkHasAdminRole() {
    for (GrantedAuthority grantedAuthority : SecurityPrincipalContext.getUserDetails().getAuthorities()) {
      if (grantedAuthority.getAuthority().equals(SysConstants.PROJECT_ROLE_ADMIN.getValue())) {
        return true;
      }
    }
    return false;
  }

  public static boolean checkHasGeneralAdminRole() {
    for (GrantedAuthority grantedAuthority : SecurityPrincipalContext.getUserDetails().getAuthorities()) {
      if (grantedAuthority.getAuthority().equals(SysConstants.PROJECT_ROLE_GENERALADMIN)) {
        return true;
      }
    }
    return false;
  }
  //  public static SysUserEntity getCurrentSysUser() {
  //    return (SysUserEntity)getAuthentication().getPrincipal();
  //  }

  //  public static Set<SysRoleEntity> getMaxLevelUserRoleSet() {
  //    List<SysRoleEntity> roleList = SysUserUtils.getCurrentSysUser().getRoles();
  //    //        账户有多个权限则按最大的算 如果权限等级相同全部获得
  //    HashSet<SysRoleEntity> set = new HashSet<SysRoleEntity>();
  //    SysRoleEntity bigRole = null;
  //    if (roleList.size() > 0) {
  //      bigRole = roleList.get(0);
  //      set.add(bigRole);
  //      for (int i = 1; i < roleList.size(); i++) {
  //        SysRoleEntity roleNext = roleList.get(i);
  //        if (bigRole.getLevel() > roleNext.getLevel()) {
  //          bigRole = roleNext;
  //          set.clear();
  //          set.add(bigRole);
  //        } else if (bigRole.getLevel() == roleNext.getLevel()) {
  //          bigRole = roleNext;
  //          set.add(bigRole);
  //        }
  //      }
  //      return set;
  //    }
  //    return null;
  //  }
}
